package LeetCode刷题.暑假刷的算法;

import java.util.LinkedList;

/**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2021-07-19 08:30
 * @description: Functions of this class is 用两个栈去实现一个队列的功能
 **/
public class 两个栈实现队列功能 {
    private static class sstack {
        //先定义两个栈
        LinkedList<Integer> stack1 = new LinkedList<>();//实现队列入队功能
        LinkedList<Integer> stack2 = new LinkedList<>();//实现队列出队功能

        public Integer pop() {
            if (stack2.isEmpty()) {//如果栈空了，则将另一个栈中的元素入到这个栈中，这样改栈出队的顺序就对了
                while (!stack1.isEmpty()) {
                    stack2.push(stack1.pop());
                }
            }
            return stack2.pop();
        }
        public void push(Integer x){
            stack1.push(x);
        }
    }

    public static void main(String args[]) {
        sstack q=new sstack();
        q.push(1);
        q.push(2);
        q.push(3);
        q.push(4);
        q.push(5);
        //出队
        System.out.println("队中元素为：");
        System.out.println(q.pop());
        System.out.println(q.pop());
        System.out.println(q.pop());
        System.out.println(q.pop());
        System.out.println(q.pop());
    }
}